package com.fortune.controller;

import com.fortune.base.BaseAPIExcution;
import com.fortune.base.BaseController;
import com.fortune.common.redis.RedisClient;
import com.fortune.constants.redis.CacheKeys;
import com.fortune.dto.excution.SalesManExcution;
import com.fortune.dto.in.cOrderLogin.COrderEditNameDto;
import com.fortune.dto.in.cOrderLogin.COrderEditPhoneDto;
import com.fortune.dto.in.cOrderLogin.COrderLoginDto;
import com.fortune.dto.in.cOrderLogin.COrderLoginResetPasswordDto;
import com.fortune.dto.in.salesMan.SalesManChangePasswordDto;
import com.fortune.entity.PhoneAndPassword;
import com.fortune.enums.SalesManEnum;
import com.fortune.service.COrderLoginService;
import com.fortune.util.TokenUtil;
import com.fortune.vo.COrderLogin.COrderLoginInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;



@Api(value = "wechat/cOrderLogin", description = "客户账户", tags = {"微信小程序（客户）：账户相关"})
@RestController
@RequestMapping("wechat/cOrderLogin")
@Validated
public class COrderLoginController extends BaseController{
	@Autowired
	private RedisClient redisClient;

	@Autowired
	private COrderLoginService cOrderLoginService;

	@ApiOperation(value = "客户登录", nickname = "cOrderLogin", notes = "", tags = {"@郭阳"})
	@PostMapping(value = "login",
			produces = { MediaType.APPLICATION_JSON_VALUE },
			consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<COrderLoginInfo> login(@Validated @RequestBody COrderLoginDto cOrderLoginDto) {
		String phone = cOrderLoginDto.getPhone();
		String password = cOrderLoginDto.getPassword();
		//验证手机号是否存在  PhoneAndPassword这里有厂商和经销商id
		PhoneAndPassword finPhoneIsExist = cOrderLoginService.finPhoneIsExist(phone);
		if(finPhoneIsExist == null){
			return new SalesManExcution<COrderLoginInfo>(null, SalesManEnum.ACCOUNT_IS_NOT_EXIST);
		}
		//验证密码是否正确
		String passwords = finPhoneIsExist.getPassword();
		if(!password.equals(passwords)){
			return new SalesManExcution<COrderLoginInfo>(null, SalesManEnum.PASSWORD_ERROR);
		}
		//验证账号是否被禁用
		String status = finPhoneIsExist.getStatus();
		if(status.equals("NO")){
			return new SalesManExcution<COrderLoginInfo>(null, SalesManEnum.ACCOUNT_IS_DISABLED);
		}
		String mid = finPhoneIsExist.getMid();
		String did = finPhoneIsExist.getDid();
		String id = finPhoneIsExist.getId();
		//获取随机唯一字符串
		String token = TokenUtil.genToken();
		String loginToken = id + "," + mid + "," + did + "," + phone;
		//key CacheKeys+随机字符串 value 手机号 存到redis里
		redisClient.save(loginToken, 60*24, CacheKeys.SALES_MAN_LOGIN_TOKEN, token);
		COrderLoginInfo findCOrderLoginInfo = cOrderLoginService.findCOrderLoginInfo(phone);
		findCOrderLoginInfo.setToken(token);
		return new SalesManExcution<COrderLoginInfo>(findCOrderLoginInfo, SalesManEnum.LOGIN_SUCCESS);
	}

	@ApiOperation(value = "客户重置密码", nickname = "cOrderResetPassword", notes = "", tags={ "@郭阳" })
	@PostMapping(value = "password/reset",
			produces = { MediaType.APPLICATION_JSON_VALUE },
			consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> resetPassword(@Validated @RequestBody COrderLoginResetPasswordDto cOrderLoginResetPasswordDto) {
		String mid = getMId();
		String did = getDId();
		String userId = getUserId();
		String phone = getPhone();
		String codeInfor = "123456";
		String code = cOrderLoginResetPasswordDto.getCode();
		if(!codeInfor.equals(code)){
			return new SalesManExcution<Void>(null, SalesManEnum.CODE_ERROR);
		}
		String password = cOrderLoginResetPasswordDto.getPassword();
		int editResetPassword = cOrderLoginService.editResetPassword(mid,did,userId,phone,password);
		if(editResetPassword < 1){
			return new SalesManExcution<Void>(null, SalesManEnum.EDIT_PASSWORD_ERROR);
		}
		return new SalesManExcution<Void>(null, SalesManEnum.EDIT_PASSWORD_SUCCESS);
	}

	@ApiOperation(value = "客户修改密码", nickname = "cOrderChangePassword", notes = "", tags={ "@郭阳" })
	@ApiImplicitParams({
			@ApiImplicitParam(name="Authorization", value="登录返回的token值",paramType="header",dataType="String",required = true)
	})
	@PostMapping(value = "password/change",
			produces = { MediaType.APPLICATION_JSON_VALUE },
			consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> changePassword(@Validated @RequestBody SalesManChangePasswordDto salesManChangePasswordDto) {
		String mid = getMId();
		String did = getDId();
		String userId = getUserId();
		String phone = getPhone();
		String oldPassword = salesManChangePasswordDto.getOldPassword();
		/*根据手机号查询密码*/
		String findPasswordByPhone = cOrderLoginService.findPasswordByPhone(mid, did, userId ,phone);
		if(!findPasswordByPhone.equals(oldPassword)){
			return new SalesManExcution<Void>(null, SalesManEnum.PASSWORD_ERROR);
		}
		String newpassword = salesManChangePasswordDto.getNewPassword();
		int editPasswordByPhone = cOrderLoginService.editPasswordByPhone(mid, did ,userId, phone, newpassword);
		if(editPasswordByPhone < 0){
			return new SalesManExcution<Void>(null, SalesManEnum.VERIFICATION_ERROR);
		}
		return new SalesManExcution<Void>(null, SalesManEnum.VERIFICATION_SUCCESS);
	}

	@ApiOperation(value = "客户修改姓名", nickname = "cOrderEditName", notes = "", tags={ "@郭阳" })
	@ApiImplicitParams({
		@ApiImplicitParam(name="Authorization", value="登录返回的token值",paramType="header",dataType="String",required = true)
	})
	@PostMapping(value = "editName",
			produces = { MediaType.APPLICATION_JSON_VALUE },
			consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> editName(@Validated @RequestBody COrderEditNameDto cOrderEditNameDto) {
		String mid = getMId();
		String did = getDId();
		String userId = getUserId();
		String name = cOrderEditNameDto.getName();
		int editCOrderName = cOrderLoginService.editCOrderName(mid, did, userId, name);
		if(editCOrderName < 1){
			return new SalesManExcution<Void>(null, SalesManEnum.EDIT_PASSWORD_ERROR);
		}
		return new SalesManExcution<Void>(null, SalesManEnum.EDIT_PASSWORD_SUCCESS);
	}

	@ApiOperation(value = "客户验证手机号", nickname = "cOrderVeriPhone", notes = "", tags={ "@郭阳" })
	@ApiImplicitParams({
			@ApiImplicitParam(name="Authorization", value="登录返回的token值",paramType="header",dataType="String",required = true)
	})
	@PostMapping(value = "veriPhone",
			produces = { MediaType.APPLICATION_JSON_VALUE },
			consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> veriPhone(@Validated @RequestBody COrderEditPhoneDto cOrderEditPhoneDto) {
		String mid = getMId();
		String did = getDId();
		String userId = getUserId();
		String phone = cOrderEditPhoneDto.getPhone();
		String code = cOrderEditPhoneDto.getCode();
		String codeInfo = "123456";
		if(!code.equals(codeInfo)){
			return new SalesManExcution<Void>(null, SalesManEnum.VERIFICATION_ERROR);
		}
		return new SalesManExcution<Void>(null, SalesManEnum.VERIFICATION_SUCCESS);
	}

	@ApiOperation(value = "客户修改手机号", nickname = "cOrderVeriNewPhone", notes = "", tags={ "@郭阳" })
	@ApiImplicitParams({
			@ApiImplicitParam(name="Authorization", value="登录返回的token值",paramType="header",dataType="String",required = true)
	})
	@PostMapping(value = "veriNewPhone",
			produces = { MediaType.APPLICATION_JSON_VALUE },
			consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> veriNewPhone(@Validated @RequestBody COrderEditPhoneDto cOrderEditPhoneDto) {
		String mid = getMId();
		String did = getDId();
		String userId = getUserId();
		String phone = cOrderEditPhoneDto.getPhone();
		String code = cOrderEditPhoneDto.getCode();
		String codeInfo = "123456";
		if(!code.equals(codeInfo)){
			return new SalesManExcution<Void>(null, SalesManEnum.VERIFICATION_ERROR);
		}
		int editNewPhone = cOrderLoginService.editNewPhone(mid, did ,phone, userId);
		if(editNewPhone < 1){
			return new SalesManExcution<Void>(null, SalesManEnum.EDIT_PHONE_ERROR);
		}
		return new SalesManExcution<Void>(null, SalesManEnum.VERIFICATION_SUCCESS);
	}
}
